<template>
  <el-dialog :visible.sync="visible" :before-close="beforeClose" title="学员订课" :close-on-click-modal="false" @open="onOpen">
    <el-form ref="form" :model="bookForm" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="10">
          <el-form-item label="学员姓名：">
            <el-input v-model="studentName" :readonly="true" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="可订课时：">
            {{ avaiableBookHour }} 节
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row>
        <el-col :span="24">
          <el-form-item label="是否扩科" prop="extra">
            <el-checkbox v-model="bookForm.extra" @change="onExtraChanged">扩科</el-checkbox>
          </el-form-item>
        </el-col>
      </el-row> -->
      <el-row>
        <el-col :span="10">
          <el-form-item label="报名课程：" prop="orderCourseId">
            <el-select v-model="bookForm.orderCourseId" clearable placeholder="选择订购课程" @change="onCourseChange" @clear="onCourseClear">
              <el-option v-for="item in courseList" :key="item.orderCourseId" :label="item.courseName" :value="item.orderCourseId">
                <span style="float: left">{{ item.courseName }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="课程单价：">
            {{ coursePrice }} 元
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="订购课时" prop="bookHour">
            <el-input-number v-model="bookForm.bookHour" :min="0" :max="avaiableBookHour" controls-position="right"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="订课备注" prop="remark">
            <el-input
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4}"
              placeholder="请输入内容"
              v-model="bookForm.remark">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listAllCourse } from '@/api/course'
import { bookCourse, getBookCourses } from '@/api/student'

export default {
  name: 'BookCourse',
  props: {
    merchantStudentId: {
      type: Number,
      required: false
    },
    studentName: {
      type: String,
      required: false
    }
  },
  data() {
    return {
      loading: false,
      visible: false,
      courseList: [],
      bookCourseList: [],
      avaiableBookHour: 0,
      coursePrice: 0,
      // 消耗的订购金
      bookForm: {
        merchantStudentId: '',
        orderCourseId: '',
        courseId: '',
        extra: false,
        bookHour: 0,
        remark: ''
      },
      rules: {
        orderCourseId: [
          { required: true, message: '请选择报名课程', trigger: 'blur' }
        ],
        bookHour: [
          { required: true, type: 'number', message: '不能为0', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onOpen() {
      // 获取用户的订购的课程信息
      console.log('打开订课面板')
      getBookCourses({ merchantStudentId: this.merchantStudentId }).then(res => {
        this.bookCourseList = res.data
        this.courseList = this.bookCourseList
      })
    },
    resetForm() {
      this.visible          = false;
      this.avaiableBookHour = 0
      this.coursePrice      = 0
      this.$refs['form'].resetFields()
    },
    onExtraChanged() {
      console.log('更改扩科状态')
      if (this.bookForm.extra === true) {
        // 有扩科
        listAllCourse().then(res => {
          this.courseList = res.data
        })
      } else {
        this.courseList = this.bookCourseList
      }
    },
    onCourseChange(index) {
      console.log('报名课程orderCourseId=' + index)
      this.bookForm.orderCourseId = index
      const list = this.courseList
      for (let i = 0; i < list.length; i++) {
        if (index != null) {
          this.bookForm.courseId= list[i].courseId
          this.coursePrice      = list[i].coursePrice
          this.avaiableBookHour =list[i].courseHour - list[i].bookHour
        }
      }
    },
    onCourseClear() {
      this.coursePrice = 0
      this.avaiableBookHour = 0
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.bookForm.merchantStudentId = this.merchantStudentId
          const para = Object.assign({}, this.bookForm)
          this.loading = true
          bookCourse(para).then(res => {
            this.$message({
              message: '学员订课成功',
              type: 'success'
            })
            this.resetForm()
            this.loading  = false
            this.visible  = false
            this.$parent.init()
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    cancel() {
      this.resetForm()
    },
    beforeClose() {
      this.resetForm()
    },
    resetForm() {
      this.visible = false;
      this.bookForm = {
        merchantStudentId: '',
        orderCourseId: '',
        courseId: '',
        bookHour: 0,
        remark: ''
      },
      this.$refs['form'].resetFields()
    }
  }
}
</script>

<style>

</style>